package ftp

import (
	"fmt"
	"gin/model"
	"strconv"
	"strings"
	"time"
)

// 登录日志
func (*DataParsing) parsingViolationsLog(catData []string, ftpInfoId int64) []model.DataViolations {
	var logDate, LogTime, steamId, gameName, gameId, logType, typeNum, logJosn, dataTxt string
	var end int
	var dataArr []model.DataViolations
	//fmt.Println("违规原始数据数组", catData)
	for _, logTxt := range catData {
		//fmt.Println("违规原始数据", logTxt)

		parts := strings.Split(logTxt, ":")
		if len(parts) < 2 {
			continue
		}
		//fmt.Println("分割数据", parts)
		timeParts := strings.Split(parts[0], "-")
		if len(timeParts) < 2 {
			continue
		}
		//fmt.Println("分割数据2", timeParts)
		//日期转换  年-月-日
		logDate = strings.Replace(timeParts[0], ".", "-", -1)
		//fmt.Println("转换数据1", catDate)
		//时间转换  时:分:秒
		Time := strings.Replace(timeParts[1], ".", ":", -1)
		//fmt.Println("转换数据2", catTime)
		layout := "2006-01-02 15:04:05"
		// 解析时间字符串
		//fmt.Println("解析时间格式为", catDate+" "+catTime)
		t, err := time.Parse(layout, logDate+" "+Time)
		if err != nil {
			fmt.Println("违规原始数据", logTxt)

			fmt.Println("解析时间格式为", err.Error())
			LogTime = "0"
			continue
			//return
		}
		// 转换为 Unix 时间戳（秒）

		LogTime = strconv.FormatInt(t.Unix(), 10)
		logTime, _ := strconv.ParseInt(LogTime, 10, 64)
		//太平洋时间制  + 8 小时
		//logTime = logTime + 3600*8
		t = time.Unix(logTime, 0)
		logDate = t.Format("2006-01-02")

		hour, minute, second := t.Clock()
		Time = fmt.Sprintf("%02d:%02d:%02d", hour, minute, second)

		//通用违规获取   : Ammo count violation detected for user ->
		start := strings.Index(logTxt, ": ")
		if start < 0 {
			logType = ""
		} else {
			// 查找第二个单引号的位置
			end = strings.Index(logTxt[start+2:], " ->")
			if end < 0 {
				logType = ""
			} else {
				logType = logTxt[start+2 : start+2+end]
				typeNum = "1"
				//加一个空格和字符本身数
				dataTxt = logTxt[start+2+end+4:]
			}
		}

		if logType == "" {

			//通用2违规获取   AConZGameMode::KickPlayer
			start := strings.Index(logTxt, ": ")
			if start < 0 {
				logType = ""
			} else {
				// 查找第二个单引号的位置
				end = strings.Index(logTxt[start+2:], "yer:")
				if end < 0 {
					logType = ""
				} else {
					logType = logTxt[start+2 : start+2+end+3]

					dataTxt = logTxt[start+2+end+4+1:]
				}
				typeNum = "2"
			}

		}

		if logType == "" {

			//通用3违规获取 [VehicleCheat][Flying]
			start := strings.Index(logTxt, "[")
			if start < 0 {
				logType = ""
			} else {
				// 查找第二个单引号的位置
				end = strings.Index(logTxt[start+1:], "] ")
				if end < 0 {
					logType = ""
				} else {
					logType = logTxt[start+1 : start+1+end+1]
					// 96 行 位置  + 1个空格
					dataTxt = logTxt[start+1+end+1+1:]
					typeNum = "3"
				}
			}

		}

		if logType == "" {

			//通用3违规获取  #Teleport
			start := strings.Index(logTxt, ": #")
			if start < 0 {
				logType = ""
			} else {
				// 查找第二个单引号的位置
				end = strings.Index(logTxt[start+3:], " ")
				if end < 0 {
					logType = ""
				} else {
					logType = logTxt[start+3 : start+3+end]

					dataTxt = logTxt[start+3+end:]
					typeNum = "4"
				}
			}

		}

		switch typeNum {
		case "1":
			ViolationsTypeALog(dataTxt)
			break
		case "2":
			break
		case "3":
			break
		case "4":
			break
		default:
			dataTxt = logTxt
			fmt.Println("出现其他类型日志")
		}

		//fmt.Println("数据为：", ftpInfoId, logDate, Time, LogTime, steamId, gameName, gameId, loginType, loginIp, location)
		//fmt.Println("数据为：", logDate)
		gameId, _ := strconv.ParseInt(gameId, 10, 64)
		downloadFile := model.DataViolations{FtpInfoId: ftpInfoId, Date: logDate, Time: Time, LogTime: logTime, SteamId: steamId, GameName: gameName, GameId: gameId, Type: logType, LogJson: logJosn, LogTxt: dataTxt}
		dataArr = append(dataArr, downloadFile)
	}
	return dataArr
}

func ViolationsTypeALog(logData string) {
	//处理解析日志 返回解析类型的json
}
